﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Person.DS;
using System.Data.OleDb;

namespace Person.Classes
{
    public class Persona:IComparable<Persona>
    {
        int id;
        string fname;
        string lname;
        int age;

        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public string Fname 
        {
            get
            {
                return fname;
            }
            set
            {
                fname = value;
            }
        }
        public string Lname
        {
            get
            {
                return lname;
            }
            set
            {
                lname = value;
            }
        }
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
        public Persona() { }
        public Persona(int _id, string _fname, string _lname, int _age)
        {
            ID = _id;
            Fname = _fname;
            Lname = _lname;
            Age = _age;
        }
        public int CompareTo(Persona  other)
        {
            return ID.CompareTo(other.ID);
        }

        ///------------------------------------------------------------
        ///CSV
        ///
        public static string MakeCSVStrFromPersona(Persona  per)
        {
            return string.Format("{0},{1},{2},{3}", per.ID.ToString(), per.Fname,per.Lname,per.Age.ToString());
        }
        public static Persona GetPersonaFromCSVStr(string str)
        {
            char[] p = { ',' };
            string[] line = str.Split(p);
            Persona per = new Persona(Convert.ToInt32(line[0]), line[1], line[2], Convert.ToInt32(line[3]));
            return per;
        }

        ///------------------------------------------------------------
        ///DB
        ///
        public static void MakeDBStrFromPersona(Persona  per)
        {
            DS_DB.Connection.Open();
            OleDbCommand command = DS_DB.Connection.CreateCommand();
            command.CommandText = "INSERT Persona (ID, Fname, Lname, Age) Values (" + per.ID + ",\'" + per.Fname + "\'" + ",\'" + per.Lname + "\'" + "," + per.Age + ")";
            command.ExecuteNonQuery();
            DS_DB.Connection.Close();
        }
        public static Persona GetPersonaFromDB(OleDbDataReader reader)
        {
            return new Persona((int)reader["ID"], reader["Fname"].ToString(), reader["Lname"].ToString(), (int)reader["Age"]);            
           
        }
    }
}
